﻿using System;
using System.Configuration;
using UDS.Configuration;
using UDS.Entity;

namespace UDS
{
    public static class UDSConfig
    {
        private static bool? s_hideDisabledFeatures;

        /// <summary>
        /// Indicates whether the disabled featuers will be hidden or not?
        /// </summary>
        public static bool HideDisabledFeatures
        {
            get
            {
                if (!s_hideDisabledFeatures.HasValue)
                {
                    s_hideDisabledFeatures = GetBooleanConfig("HideDisabledFeatures", true);
                }

                return s_hideDisabledFeatures.Value;
            }
        }

#if DEBUG
        private static bool? s_allowMultipleLogon;
        private static bool? s_hidePageHeader;

        /// <summary>
        /// Indicate whether a user can logon to the system on several computers at the same time.
        /// The value will always be true on RELEASE builds.
        /// </summary>
        public static bool AllowMultipleLogon
        {
            get
            {
                if (!s_allowMultipleLogon.HasValue)
                {
                    s_allowMultipleLogon = GetBooleanConfig("DBG_AllowMultipleLogon", false);
                }
                return s_allowMultipleLogon.Value;
            }
        }

        /// <summary>
        /// Whether to display or hide the page header.
        /// </summary>
        public static bool HidePageHeader
        {
            get
            {
                if (!s_hidePageHeader.HasValue)
                {
                    string var = Environment.GetEnvironmentVariable("UDS_HIDE_PAGE_HEADER");
                    bool b;
                    if (!bool.TryParse(var, out b))
                    {
                        b = false;  // display the header by default.
                    }
                    s_hidePageHeader = b;
                }
                return s_hidePageHeader.Value;
            }
        }
#else
        public const bool AllowMultipleLogon = false;
        public const bool HidePageHeader = false;
#endif

        /// <summary>
        /// Determines whether the specified feature is enabled or not.
        /// </summary>
        public static bool IsFeatureEnabled(int featureId)
        {
            string value = ConfigurationManager.AppSettings["F" + featureId];
            if (value == null)
            {
                return false;
            }

            return value == "1";
        }

        public static ClassType GetCurrentAppType()
        {
            UDSSection section = (UDSSection)ConfigurationManager.GetSection("udsApp");
            if (section == null)
            {
                throw new ConfigurationErrorsException("The configuration section 'udsApp' is missing.");
            }
            return section.ClassType;
        }

        private static bool GetBooleanConfig(string key, bool defaultValue)
        {
            bool value;
            if (!bool.TryParse(ConfigurationManager.AppSettings[key], out value))
            {
                value = defaultValue;
            }

            return value;
        }
    }
}
